using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ent.Web.Helper;
using Ent.Voice.Nomination;
using System.Collections.Generic;
using Ent.Web;
using Ent.Web.Skinner;
using Ent.Voice.Show;

public partial class UpdateShow : PageSkinner, IPageWizard
{
    public string GetTitle(Dictionary<string, object> values)
    {
        return (values != null && values.ContainsKey(EventProperty.Title) ? values[EventProperty.Title].ToString() : "Update event");
    }
    
    public string SuccessCode { get { return "EventUpdated"; } }
    public string ErrorCode { get { return "ErrorEventUpdation"; } }
    public string Operation 
    { 
        get
        {
            return "Update Show";
        } 
    }
    public bool CanProcessWizard
    {
        get
        {
            try
            {
                int id = MathFunction.ConvertToInteger(Request.QueryString["id"]);
                int total = 0;
                Event evt = Event.GetEvent(id, 0, 0, false, StringHelper.ConvertToString(Session["USERID"]), ref total);
                Skin_wizard wiz = ((Skin_wizard)this.Master);
                if (evt.Organiser.UserID.ToLower() != Session["USERID"].ToString().ToLower())
                {
                    wiz.SetMessage(Common.GetErrorText(ResourceHelper.Get("MSG_Evt_AccessDenied")));
                    return false;
                }
                wiz.Values.Add(EventProperty.EventId, id);
                wiz.Values.Add(EventProperty.Access, new string[] { Convert.ToString((int)evt.Access) });
                wiz.Values.Add(EventProperty.AccessCode, evt.AccessCode);
                wiz.Values.Add(EventProperty.AccessPublic, StringHelper.BitToArray((int)evt.AccessPublic));
                wiz.Values.Add(EventProperty.Catagory, evt.CatagoryId);
                wiz.Values.Add(EventProperty.Count, (evt.MaxVoteCount != -1));
                wiz.Values.Add(EventProperty.Criteria, evt.Criteria);
                wiz.Values.Add(EventProperty.Description, evt.Description);
                wiz.Values.Add(EventProperty.DueDate, evt.EndDate.ToShortDateString());
                if (evt.UploadFileTypes != null)
                {
                    string[] ft = new string[evt.UploadFileTypes.Count];
                    for (int i = 0; i < evt.UploadFileTypes.Count; i++)
                    {
                        ft.SetValue(Convert.ToString((int)evt.UploadFileTypes[i]), i);
                    }
                    wiz.Values.Add(EventProperty.FileType, ft);
                }
                wiz.Values.Add(EventProperty.Fixed, (evt.EndDate > Event.MinDate));
                wiz.Values.Add(EventProperty.Mandatory, StringHelper.BitToArray((int)evt.MandateField));
                //wiz.Values.Add(EventProperty.MaxPart, evt.);
                wiz.Values.Add(EventProperty.MaxPartLimit, evt.MaxNominationCount);
                wiz.Values.Add(EventProperty.MaxVoteCount, evt.MaxVoteCount);
                wiz.Values.Add(EventProperty.MinSponsor, evt.MinSponsorAmt);
                wiz.Values.Add(EventProperty.NCurrency, (int)evt.Currency);
                wiz.Values.Add(EventProperty.NomAmount, evt.FeeAmount);
                wiz.Values.Add(EventProperty.NomineeTracking, StringHelper.BitToArray((int)evt.NominationTrack));
                wiz.Values.Add(EventProperty.Participation, evt.AllowNomination);
                wiz.Values.Add(EventProperty.ParticipationStDate, evt.NominationStartDate.ToShortDateString());
                wiz.Values.Add(EventProperty.SCurrency, evt.Currency);
                wiz.Values.Add(EventProperty.SkinId, evt.SkinId);
                wiz.Values.Add(EventProperty.Sponcership, evt.AllowSponsorship);
                wiz.Values.Add(EventProperty.SponcerStData, evt.SponsorStartDate.ToShortDateString());
                wiz.Values.Add(EventProperty.StartDate, evt.StartDate.ToShortDateString());
                wiz.Values.Add(EventProperty.Title, evt.Name);
                wiz.Values.Add(EventProperty.Upload, evt.AllowFileUpload);
                wiz.Values.Add(EventProperty.VoterTracking, StringHelper.BitToArray((int)evt.VoterTrack));
                wiz.Values.Add(EventProperty.VoteStDate, evt.VoteStartDate.ToShortDateString());
                return true;
            }
            catch (Exception exc)
            {
                Common.ErrorPage(exc);
            }
            return false;
        }
    }
    bool IsPremiumApplicable
    {
        get
        {
            return false;
        }
    }
    public void SetWizard(Ent.Web.WebControl.UserControl c)
    {
        string mode = Request.QueryString["mode"];
        Dictionary<string, string> prop = new Dictionary<string, string>();
        switch (mode)
        {
            case "2":
                if (IsPremiumApplicable)
                {
                    prop = Priviledge.GetProperties("premier");
                    c.AddASPXParam("isPremium", true);

                }
                else
                    throw new Exception("InsufficientCredit");
                break;
            default:
            case "1":
                prop = Priviledge.GetProperties("general");
                c.AddASPXParam("isPremium", false);
                break;
        }
        c.AddASPXParam("maxVoteCount", (prop.ContainsKey("votecount") ? MathFunction.ConvertToInteger(prop["votecount"], 1000) : 1000));
        c.AddASPXParam("maxNomCount", (prop.ContainsKey("nominationcount") ? MathFunction.ConvertToInteger(prop["nominationcount"], 25) : 25));
        int dur = (prop.ContainsKey("maxdaterange") ? MathFunction.ConvertToInteger(prop["maxdaterange"], 60) : 60);
        c.AddASPXParam("maxDuration", Math.Round((double)dur / 30));
    }
    public bool ProcessWizardData(Dictionary<string, object> values)
    {
        try
        {
            string mode = Request.QueryString["mode"];
            string feature = (mode == "2" ? (IsPremiumApplicable ? "premier" : "general") : "general");
            Dictionary<string, string> eventProperty = Priviledge.GetProperties(feature);
            Event e = new Event();
            e.Id = Convert.ToInt32(values[EventProperty.EventId]);
            e.Organiser = new Ent.Voice.User();
            e.Organiser.UserID = Session["USERID"].ToString();

            e.CatagoryId = Convert.ToInt32(values[EventProperty.Catagory]);
            e.Name = values[EventProperty.Title].ToString();
            e.Description = values[EventProperty.Description].ToString();
            e.StartDate = Convert.ToDateTime(values[EventProperty.StartDate]);
            e.EndDate = Convert.ToDateTime(values[EventProperty.DueDate]);
            e.AccessCode = values[EventProperty.AccessCode].ToString();
            #region Access
            string[] access = (string[])values[EventProperty.Access];
            if (access.Length > 0)
            {
                e.Access = (EventAccess)Enum.Parse(typeof(EventAccess), access[0].ToString());
            }
            #endregion
            #region AccessPublic
            string[] list = (string[])values[EventProperty.AccessPublic];
            int i = 0;
            foreach (string item in list)
            {
                i += Convert.ToInt32(item);
            }
            switch (i)
            {
                case 0:
                    e.AccessPublic = EventAccessPublic.None;
                    break;
                case 1:
                    e.AccessPublic = EventAccessPublic.PublicViewable;
                    break;
                case 2:
                    e.AccessPublic = EventAccessPublic.PublicParticipable;
                    break;
                case 3:
                    e.AccessPublic = EventAccessPublic.All;
                    break;
            }
            #endregion

            e.AllowNomination = values.ContainsKey(EventProperty.Participation) ? Convert.ToBoolean(values[EventProperty.Participation]) : false;
            e.NominationStartDate = (e.AllowNomination ? Convert.ToDateTime(values[EventProperty.ParticipationStDate]) : Event.MinDate);
            #region NominationTracking
            if (e.AllowNomination)
            {
                list = (string[])values[EventProperty.NomineeTracking];
                i = 0;
                foreach (string item in list)
                {
                    i += Convert.ToInt32(item);
                }
                switch (i)
                {
                    case 0:
                        e.NominationTrack = Track.None;
                        break;
                    case 1:
                        e.NominationTrack = Track.IP;
                        break;
                    case 2:
                        e.NominationTrack = Track.Email;
                        break;
                    case 3:
                        e.NominationTrack = Track.All;
                        break;
                }
            }
            else
                e.NominationTrack = Track.None;

            #endregion
            #region MaximumNomination
            if (e.AllowNomination)
            {
                string[] maxPart = (string[])values[EventProperty.MaxPart];
                if (maxPart.Length > 0)
                {
                    EventParticipation ePart = (EventParticipation)Enum.Parse(typeof(EventParticipation), maxPart[0].ToString());
                    e.MaxNominationCount = (ePart == EventParticipation.Default ? MathFunction.ConvertToInteger(eventProperty["nominationcount"], 25) : Convert.ToInt32(values[EventProperty.MaxPartLimit]));
                }
            }
            #endregion
            e.FeeAmount = Convert.ToInt32(values[EventProperty.NomAmount]);
            e.Currency = (EventNomCurrency)Enum.Parse(typeof(EventNomCurrency), values[EventProperty.NCurrency].ToString());
            #region FileType
            e.AllowFileUpload = values.ContainsKey(EventProperty.Upload) ? Convert.ToBoolean(values[EventProperty.Upload]) : false;
            if ((bool)values[EventProperty.Upload])
            {
                e.UploadFileTypes = new List<EventFileType>();
                list = (string[])values[EventProperty.FileType];
                foreach (string item in list)
                {
                    e.UploadFileTypes.Add((EventFileType)Enum.Parse(typeof(EventFileType), item));
                }
            }
            #endregion

            e.VoteStartDate = Convert.ToDateTime(values[EventProperty.VoteStDate]);
            #region MandateField
            list = (string[])values[EventProperty.Mandatory];
            i = 0;
            foreach (string item in list)
            {
                i += Convert.ToInt32(item);
            }
            switch (i)
            {
                case 0:
                    e.MandateField = EventMandate.None;
                    break;
                case 1:
                    e.MandateField = EventMandate.Name;
                    break;
                case 2:
                    e.MandateField = EventMandate.EMail;
                    break;
                case 3:
                    e.MandateField = EventMandate.All;
                    break;
            }
            #endregion
            #region VoterTracking
            list = (string[])values[EventProperty.VoterTracking];
            i = 0;
            foreach (string item in list)
            {
                i += Convert.ToInt32(item);
            }
            switch (i)
            {
                case 0:
                    e.VoterTrack = Track.None;
                    break;
                case 1:
                    e.VoterTrack = Track.IP;
                    break;
                case 2:
                    e.VoterTrack = Track.Email;
                    break;
                case 3:
                    e.VoterTrack = Track.All;
                    break;
            }
            #endregion
            e.MaxVoteCount = (Convert.ToBoolean(values[EventProperty.Count]) ? Convert.ToInt32(values[EventProperty.MaxVoteCount]) : -1);

            e.AllowSponsorship = values.ContainsKey(EventProperty.Sponcership) ? Convert.ToBoolean(values[EventProperty.Sponcership]) : false;
            e.SponsorStartDate = (e.AllowSponsorship ? Convert.ToDateTime(values[EventProperty.SponcerStData]) : Event.MinDate);
            e.MinSponsorAmt = (e.AllowSponsorship ? Convert.ToInt32(values[EventProperty.MinSponsor]) : -1);

            e.SkinId = values.ContainsKey(EventProperty.SkinId) ? Convert.ToInt32(values[EventProperty.SkinId]) : -1;

            e.Invitees = values.ContainsKey(EventProperty.Invitees) ? values[EventProperty.Invitees].ToString() : "";
            e.Criteria = (Dictionary<string, string>)values[EventProperty.Criteria];
            EventResponse res = Event.Update(e);

            if (res == EventResponse.EventUpdated)
            {
                Ent.Web.Mail.Processor p = new Ent.Web.Mail.Processor();
                Ent.Web.Mail.MailContent cont = Ent.Web.Mail.Collection.GetMailContent("EventUpdated");
                p.To = Session["EMAIL"].ToString();
                p.Subject = cont.Subject;
                p.Body = string.Format(cont.Body, Session["FNAME"].ToString(), e.Name, ConfigurationManager.AppSettings.Get("BaseUrl") + "/ShowEvent.aspx?id=" + e.Id.ToString());
                p.SendMail();

                string[] emails = e.Invitees.Split(new char[] { ',', ';', ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string email in emails)
                {
                    if (Ent.Web.Mail.Processor.IsValidEmail(email))
                    {
                        p = new Ent.Web.Mail.Processor();
                        cont = Ent.Web.Mail.Collection.GetMailContent("InviteNominee");
                        p.To = email;
                        p.Subject = string.Format(cont.Subject, e.Name);
                        p.Body = string.Format(cont.Body, Session["FNAME"].ToString(), e.Name, e.StartDate.ToShortDateString(), e.FeeAmount.ToString() + " " + e.Currency.ToString(), ConfigurationManager.AppSettings.Get("BaseUrl") + "/Nominate.aspx?id=" + e.Id.ToString());
                        p.SendMail();
                    }
                }

                return true;
            }
            else
                return false;
        }
        catch (Exception exc)
        {
            Common.Log.Error(exc);
            return false;
        }
        return false;
    }
}
